package light.head.controller.mobile;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.mobile.MobileCommunicationDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Communication;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 通讯录
 * @author yangguanghe @date 2014-6-4 @time 下午4:21:55
 *
 */
@Controller
@RequestMapping(Url.MOBILE_COMMUNICATION)
public class MobileCommunicationController extends SupporterController {
	
	private MobileCommunicationDAO cDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init (HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.MOBILE_COMMUNICATION_INIT);
	}
	
	@RequestMapping(Url.LIST)
	public ModelAndView list (HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.MOBILE_COMMUNICATION_LIST);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView dataList (HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String,String> map = createQueryMapByParmName(request, Parm.NAME, Parm.ST, Parm.ET, Parm.TYPE, Parm.TYPE, Parm.DELETED);
		
		cDAO.findByMap(cu_organCode(request), map, pb);
		
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MOBILE_COMMUNICATION_DATALIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		
		if (!isNotNull(code)) {
			return new ModelAndView(Url.MOBILE_COMMUNICATION_EDIT);
		}
		
		Communication c = cDAO.get(Integer.parseInt(code));
		if (null == c) return new ModelAndView(Url.MOBILE_COMMUNICATION_EDIT);
		
		request.setAttribute(Parm.INFO, c);
		
		return new ModelAndView(Url.MOBILE_COMMUNICATION_EDIT);
	}
	
	@RequestMapping(Url.SHIELD)
	public void shield (HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		Communication c = cDAO.get(Integer.parseInt(code));
		if (null == c) {
			print(response, Msg.NOT_FIND);
			return;
		}
		
		c.setDeleted(true);
		
		if (RESULT.SUCCESS == cDAO.upd(c))
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
		
	}
	
	@RequestMapping(Url.SAVE)
	public void save (Communication c, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		if (null == c) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		RESULT result = RESULT.ERROR;
		if (null != c.getCode()) {
			result = cDAO.update(c);
		} else {
			
			Date ct = new Date();
			Integer cu_code = cu_code(request);
			Integer oc_code = cu_organCode(request);
			
			c.setCt(ct);
			c.setCu(cu_code);
			c.setOc(oc_code);
			c.setDeleted(false);
			if (null == c.getType()) {
				c.setType(Sys.B_ONE); //	 默认类型为个人
			}
			
			result = cDAO.add(c);
		}
		
		if (RESULT.SUCCESS == result)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
		
	}

	
	
	@Autowired
	public void setcDAO(MobileCommunicationDAO cDAO) {
		this.cDAO = cDAO;
	}

	
}
